﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------

using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using System;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using UnityEngine.IO;
using MdxLib.Model;

namespace UnityEngine.Graphics
{
    public class UnitPaletteRenderContainer : PaletteRendererContainer
    {
        private static Game _Game;

        private static List<string> _DebugRecords;

        private static Dictionary<string, UnitRenderer> _UnitRenderer;

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_UnitRenderer != null)
                {
                    foreach (var unit in _UnitRenderer.Values)
                        if (unit != null)
                            unit.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        public UnitPaletteRenderContainer(Game game)
        {
            _Game = game;

            _UnitRenderer = new Dictionary<string, UnitRenderer>();

            string ConfigFileName = "Data\\Buildings.txt";
            using (FileStream file = new FileStream(IOManager.Instance.AssetsFolder + "\\" + ConfigFileName, FileMode.Open))
            {
                StreamReader Reader = new StreamReader(file);

                string line;
                string[] words;
                while (Reader.EndOfStream == false)
                {
                    line = Reader.ReadLine().Trim();
                    words = line.Split(new char[] { '\r', '\t', '\0', ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    if (words.Length > 1)
                    {
                        //string path = GameplayWorld.Instance.ObjectsManager.GetFiledValue(EObjectType.UnitData, words[0], "file");
                        //AddUnitRenderer(words[0], path, game);
                    }
                }
            }

            _DebugRecords = new List<string>();
        }

        public void AddUnitRenderer(string name, string ModelName, Game game)
        {
            if (_UnitRenderer.ContainsKey(name) == true)
                return;

            if (String.IsNullOrEmpty(ModelName))
            {
                _UnitRenderer.Add(name, new UnitRenderer(null, null));
            }
            else
            {
                if (Path.HasExtension(ModelName))
                    ModelName = ModelName.Substring(0, ModelName.IndexOf("."));

                CModel model = ModelRenderer.LoadCModel(IOManager.Instance.GetAssertPath(ModelName));
                //MDX.Model model = game.Content.Load<MDX.Model>(ModelName);
                _UnitRenderer.Add(name, new UnitRenderer(game.GraphicsDevice, model));
            }
        }

        public override PaletteRenderer GetPaletteRenderer(string name)
        {
            if (_UnitRenderer.ContainsKey(name) == false)
            {
                if (_DebugRecords.Contains(name) == false)
                    _DebugRecords.Add(name);
                return null;
            }
            return _UnitRenderer[name];
        }

        public static void PrintDebugRecords()
        {
            _DebugRecords.Sort();
            for (int i = 0; i < _DebugRecords.Count; i++)
                Debug.WriteLine(_DebugRecords[i]);
        }
    }
}
